package com.worldcup.service.question.impl;

import com.google.common.cache.Cache;
import com.google.common.cache.CacheBuilder;
import com.google.common.cache.CacheLoader;
import com.google.common.cache.LoadingCache;
import com.worldcup.dao.question.QuestionMapper;
import com.worldcup.model.question.Question;
import com.worldcup.service.question.QuestionService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;
import java.util.List;
import java.util.concurrent.ExecutionException;

/**
 * @author duwan.zq
 * @date 2019/03/05
 */
@Service
public class QuestionServiceImpl implements QuestionService{

    private final static Logger logger = LoggerFactory.getLogger(QuestionServiceImpl.class);

    /** question缓存 **/
    private LoadingCache<Long, Question> questionCache;

    @PostConstruct
    public void init(){
        questionCache = CacheBuilder.newBuilder().maximumSize(30).build(new CacheLoader<Long , Question>() {
            @Override
            public Question load(Long questionId){
                return questionMapper.selectByPrimaryKey(questionId);
            }
        });
    }

    @Autowired
    private QuestionMapper questionMapper;

    @Override
    public List<Question> listQuestion() {
        return questionMapper.list();
    }

    @Override
    public Question getQuestion(Long questionId){
        return questionCache.getUnchecked(questionId);
    }
}
